package com.trailwalker.traceability.util;

import com.trailwalker.javafx.core.entity.Bundle;
import com.trailwalker.javafx.core.entity.Intent;
import com.trailwalker.javafx.widget.dialog.JfxToast;
import com.trailwalker.traceability.activity.LoginActivity;
import com.trailwalker.traceability.widget.dialog.ConfirmDialog;
import com.trailwalker.traceability.widget.dialog.JfxLoadingForm;
import com.trailwalker.traceability.widget.dialog.QrCodeDialog;
import io.reactivex.functions.Action;
import javafx.scene.image.Image;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.List;

public class JfxUtils {

    /**
     * 获取加载窗口的实例
     *
     * @param stage 主窗口
     * @return 返回加载窗口的实例
     */
    public static JfxLoadingForm getLoadingForm(Stage stage) {
        return getLoadingForm(stage, "数据加载中，请稍后……");
    }

    /**
     * 获取加载窗口的实例
     *
     * @param stage 主窗口
     * @param text  加载窗口的文字内容
     * @return 返回加载窗口的实例
     */
    public static JfxLoadingForm getLoadingForm(Stage stage, String text) {
        double stageHeight = stage.getHeight();
        JfxLoadingForm jfxLoadingForm = new JfxLoadingForm(stage);
        jfxLoadingForm.setText(text)
                .setTextFill(Paint.valueOf("#ffffff"))
                .setFont(Font.font(stageHeight / 40))
                .setProgressIndicatorBackground("#ffffff")
                .setProgressIndicatorSize(stageHeight / 20)
                .setSpacing(stageHeight / 80);
        return jfxLoadingForm;
    }

    /**
     * 弹出吐司
     *
     * @param stage 主窗口
     * @param text  需要弹出的文字内容
     */
    public static void showShortToast(Stage stage, String text) {
        JfxToast.makeText(stage, text, JfxToast.LENGTH_SHORT).show();
    }

    /**
     * 弹出吐司
     *
     * @param stage 主窗口
     * @param text  需要弹出的文字内容
     */
    public static void showLongToast(Stage stage, String text) {
        JfxToast.makeText(stage, text, JfxToast.LENGTH_LONG).show();
    }

    /**
     * 获取图片的缩放比例
     *
     * @param image          源图片
     * @param imageMaxHeight 图片可达到的最大高度
     * @param imageMaxWidth  图片可达到的最大宽度
     * @return 返回图片尺寸的缩放比例
     */
    public static double getImageFitScale(Image image, double imageMaxHeight, double imageMaxWidth) {
        double imageHeight = image.getHeight();
        double imageWidth = image.getWidth();
        if (imageHeight / imageWidth >= imageMaxHeight / imageMaxWidth) {
            return imageMaxHeight / imageHeight;
        } else {
            return imageMaxWidth / imageWidth;
        }
    }

    /**
     * 显示密码错误弹窗
     *
     * @param stage
     */
    public static void showPasswordErrorDialog(Stage stage) {
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setContentText("密码错误！")
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示重复扫码弹窗
     *
     * @param stage
     */
    public static void showRepeatScanDialog(Stage stage) {
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setContentText("重复扫码")
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示未查询到产品工序弹窗
     *
     * @param stage
     */
    public static void showNotFoundProcessDialog(Stage stage) {
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setTitleText("重要提醒")
                .setContentTextColor("#ff0000")
                .setContentText("未查询到产品工序\n请联系管理人员")
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示产品未配置工序弹窗
     *
     * @param stage
     */
    public static void showNotOwnerProcessDialog(Stage stage) {
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setTitleText("重要提醒")
                .setContentTextColor("#ff0000")
                .setContentText("此产品没有这个工序")
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示工序未完成弹窗
     *
     * @param stage
     * @param processList
     */
    public static void showUncompleteProcessDialog(Stage stage, List<String> processList) {
        StringBuilder process = new StringBuilder();
        if (processList != null) {
            for (int i = 0; i < processList.size(); i++) {
                process.append(processList.get(i)).append("未完成！！！");
                if (i != processList.size() - 1) {
                    process.append("\n");
                }
            }
        }
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setTitleText("重要提醒")
                .setContentTextColor("#ff0000")
                .setContentText(process.toString())
                .setScaleTextSize(25)
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示注册成功弹窗
     *
     * @param stage
     * @param action
     */
    public static void showRegisterSuccessDialog(Stage stage, Action action) {
        ConfirmDialog.ConfirmDialogBuilder confirmDialogBuilder = new ConfirmDialog.ConfirmDialogBuilder(stage);
        ConfirmDialog confirmDialog = confirmDialogBuilder.setDialogWidth(450)
                .setContentText("注册成功！")
                .setButtonText("知道了")
                .setOnButtonClickListener(new ConfirmDialog.OnButtonClickListener() {
                    @Override
                    public void onClick(ConfirmDialog confirmDialog) {
                        confirmDialog.dismiss();
                        if (action != null) {
                            try {
                                action.run();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).build();
        confirmDialog.show();
    }

    /**
     * 显示二维码弹窗
     *
     * @param stage
     * @param url
     */
    public static void showQrCodeDialog(Stage stage, String url) {
        new QrCodeDialog.ConfirmDialogBuilder(stage)
                .setDialogHeight(500)
                .setDialogWidth(500)
                .setImageUrl(url)
                .build()
                .show();
    }
}
